import modal

app = modal.App("nfs-app")
shared_fs = modal.NetworkFileSystem.from_name("my-shared-fs", create_if_missing=True)
SHARED_DIR = "/shared"

@app.function(network_file_systems={SHARED_DIR: shared_fs}, concurrency_limit=5)
def writer(i):
    # 多个并发任务同时写入同一个文件
    with open(f"{SHARED_DIR}/log.txt", "a") as f:
        f.write(f"Log entry from writer {i}\n")

@app.local_entrypoint()
def main():
    # 启动多个并发写入任务
    for i in range(5):
        writer.spawn(i)